﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ITS.Infrastructure.Helpers;
using ITS.Infrastructure.Constants;
using ITS.Core.Domain.Users;
using ITS.Factory;
using System.Linq;
using ITS.DAL.Users;
using ITS.Core.ViewModel.Users;

namespace ITS.Website.Attributes
{
    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
        {
            return base.AuthorizeCore(httpContext);
            //if (!httpContext.User.Identity.IsAuthenticated)
            //    return false;
            //return true;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            //base.OnAuthorization(filterContext);
            if (!UserAuthorization(filterContext))
            {
                HandleUnauthorizedRequest(filterContext);
            }
        }

        public string GetUserDataByCookieName(string cookieName, HttpRequestBase request)
        {
            HttpCookie formsCookie = request.Cookies[cookieName];

            if (formsCookie != null)
            {
                FormsAuthenticationTicket auth = FormsAuthentication.Decrypt(formsCookie.Value);
                return auth.UserData;
            }

            return null;
        }

        private bool UserAuthorization(AuthorizationContext filterContext)
        {
            bool Authorized = false;
            string[] requiredRoles;
            UserSessionModel userSession = SessionHelper.GetSession<object>(SessionNames.UserSessionModel) as UserSessionModel;
            if (userSession == null)
            {
                string data = GetUserDataByCookieName(ConstantsHelper.CookieName_User.UserCookie, filterContext.HttpContext.Request);
                if (data != null)
                {
                    Guid id = Guid.Empty;
                    if (Guid.TryParse(data, out id))
                    {
                        IUserAccountRepository adminAccountRepository = NinjectRepositoriesFactory.Get<IUserAccountRepository>();
                        SessionHelper.SetSession<object>(SessionNames.UserSessionModel, adminAccountRepository.Get(id));
                    }
                }
            }
            else
            {
                requiredRoles = Array.ConvertAll(Roles.Split(','), p => p.Trim());
                if (Roles.Equals(string.Empty))
                {
                    Authorized = true;
                }
                else
                {
                    foreach (var role in userSession.Roles)
                    {
                        if (requiredRoles.Contains(role.RoleName))
                        {
                            Authorized = true;
                        }
                    }
                }
            }
            return Authorized;
        }
    }
}
